#!/usr/bin/env python
#  Copyright (C) 2008 Monadsphere <monadli@gmail.com>
#
# This file is part of Monadsphere.
#
# Monadsphere is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Monadsphere is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Monadsphere. If not, see <http://www.gnu.org/licenses/>.

from monadsphere.html.base import *

class ALink(HtmlTag.createTagType('a')):

  def setHref(self, href):
    print self + '.href=' + StrWrap(href)

  def setTarget(self, target):
    print self + '.target=' + StrWrap(target)

class JavaScript(HtmlTag.createTagType('script')):
  def setSrc(self, src):
    print self + '.type="text/javascript"'
    print self + '.src="' + src + '"'

class Button(HtmlTag.createTagType('button')):

  def setOptions(self, options):
    if (options is not None):
      HtmlElement.setOptions(self, options)
      if ('label' in options):
        self.setLabel(options['label'])

  def setLabel(self, label):
    self.setInnerHTML(label)

class Div(HtmlTag.createTagType('div')):
  pass

class FileInput(HtmlTag.createTagType('input')):
  def createHandle(self, val):
    HtmlTag.createHandle(self)
    print self + '.type="file"'

  def setOptions(self, options):
    HtmlElement.setOptions(self, options)
    if ('size' in options):
      self.setSize(options['size'])

  def setSize(self, size):
    print self + '.size=' + str(size)

class IFrame(HtmlTag.createTagType('iframe')):
  def setSrc(self, src):
    print self + '.src="' + src + '"'

class Li(HtmlTag.createTagType('li')):
  pass

class Span(HtmlTag.createTagType('span')):
  pass

class Option(HtmlTag.createTagType('option')):
  def setValue(self, value):
    print self + '.value=',qstr(value)

class Select(HtmlTag.createTagType('select')):

  def setOptions(self, options):
    if (options is not None):
      HtmlElement.setOptions(self, options)
      if ('option' in options):
        for option in set(options['option']):
          self.addOption(option)

  def addOption(self, value, text):
    opt = self.doc.createElement(Option)
    opt.setValue(value)
    opt.setInnerHTML(text)
    self.appendChild(opt)

  def getSelected(self):
    return Var(self.doc, self + '.options[' + self + '.selectedIndex]')

class TextInput(HtmlTag.createTagType('input')):

  def createHandle(self, val):
    HtmlTag.createHandle(self)
    print self + '.type="text"'

  def setOptions(self, options):
    if (options is not None):
      HtmlElement.setOptions(self, options)
      if ('size' in options):
        self.setSize(options['size'])
      if ('maxLength' in options):
        self.setMaxLength(options['maxLength'])

  def setSize(self, size):
    print self + '.size=' + str(size)

  def setMaxLength(self, size):
    print self + '.maxlength=' + str(size)

  def getValue(self):
    return Var(self.doc, self + '.value')

class Ul(HtmlTag.createTagType('ul')):
  pass
